/**
 * Copyright (c) 2018 人人开源 All rights reserved.
 *
 * https://www.renren.io
 *
 * 版权所有，侵权必究！
 */

package io.renren;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.lang.Console;
import cn.hutool.poi.excel.BigExcelWriter;
import cn.hutool.poi.excel.ExcelUtil;
import cn.hutool.poi.excel.sax.handler.RowHandler;
import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;

import java.util.List;

/**
 * 本类在次项目环境中运行会失败，jar包问题
 */
@SpringBootTest
public class GetPinExcelJavaheapspaceTest2 {


    /**
     *
     * 读取20wExcel 并写入新的Excel中
     * @throws Exception
     */
    @Test
    public void testHutoolExcelBigRead(){
//        String filePath = "D:\\devtools\\work\\PIN\\1211\\openid1213.xlsx";//1000
        String filePath20w = "D:\\devtools\\work\\PIN\\1211\\openid20w.xlsx";//1000
        System.out.println("===========");
        ExcelUtil.readBySax(filePath20w, 0, createRowHandler());
    }

    /*
    参考：
    https://hutool.cn/docs/#/poi/Excel%E8%AF%BB%E5%8F%96-ExcelReader?id=%e4%bd%bf%e7%94%a8
    https://blog.csdn.net/m0_51666421/article/details/134775120
    todo m 本工程里面 由于引入easypoi，jar包冲突，此功能在mybatis_1108工程里有实现：GetPinExcelJavaheapspaceTest.java
    * */
    private RowHandler createRowHandler() {
        return new RowHandler() {
            //todo m 输出文件必须是不存在的文件
            String writeFilePath = "D:\\devtools\\work\\PIN\\1211\\openid240611-2.xlsx";
            int maxRow = 243432;//243432 是Excel最大行数
            BigExcelWriter writer= ExcelUtil.getBigWriter(writeFilePath);
            @Override
            public void handle(int sheetIndex, long rowIndex, List<Object> rowlist) {
                Console.log("[{}] [{}] {}", sheetIndex, rowIndex, rowlist);
                //写入数据
                writer.write(rowlist);
                //maxRow 是Excel最大行数
               if(rowIndex >= maxRow ){
                   writer.close();
               }
            }
        };
    }

     /**
     * 大数据量写入Excel,20w数据量
     * @throws Exception
     */
    @Test
    public void testHutoolExcelBigWriter20w() throws Exception {
        List<?> row1 = CollUtil.newArrayList("aa", "bb", "cc", "dd", DateUtil.date(), 3.22676575765);
        List<?> row2 = CollUtil.newArrayList("aa1", "bb1", "cc1", "dd1", DateUtil.date(), 250.7676);
        List<?> row3 = CollUtil.newArrayList("aa2", "bb2", "cc2", "dd2", DateUtil.date(), 0.111);
        List<?> row4 = CollUtil.newArrayList("aa3", "bb3", "cc3", "dd3", DateUtil.date(), 35);
        List<?> row5 = CollUtil.newArrayList("aa4", "bb4", "cc4", "dd4", DateUtil.date(), 28.00);
        List<List<?>> rows = CollUtil.newArrayList(row1, row2, row3, row4, row5);
        List<List<?>> rows2 = CollUtil.newArrayList(row1, row2, row3, row4, row5);
        //todo m 输出文件必须是不存在的文件
        String writeFilePath = "D:\\devtools\\work\\PIN\\1211\\openid240611-3.xlsx";
        BigExcelWriter writer= ExcelUtil.getBigWriter(writeFilePath);
        // 一次性写出内容，使用默认样式
        writer.write(rows);
        //todo m 可以循环写入
        writer.write(rows2);
        // 关闭writer，释放内存
        writer.close();
    }



}
